package com.TencentMusic.interview;

import java.util.Scanner;

/**
 * @author ZhangyiA
 * @describe: DFS , 与 LeetCode 上的 水域 和 岛屿 问题一致
 * 只需要吧走过的点置为 0 即可
 * @date 2021/07/03
 */

public class One_ThreeKingdoms {

    public int getForcesNum(char[][] grid){
        if(grid.length == 0 || grid[0].length == 0) return 0;
        int count = 0;
        for(int i = 0 ; i < grid.length ; i ++){
            for(int j = 0 ; j < grid[0].length ; j ++){
                if(grid[i][j] != '0'){
                    count ++;
                    dfs(grid , i , j , grid[i][j]);
                }
            }
        }
        return count;
    }

    int[] dx = {-1 , 1 , 0 , 0};
    int[] dy = {0 , 0 , -1 , 1};
    public void dfs(char[][] grid , int x , int y , char ch){
        grid[x][y] = '0';
        for(int i = 0 ; i < 4 ; i ++){
            int nx = x + dx[i];
            int ny = y + dy[i];
            if(nx < 0 || nx >= grid.length || ny < 0 || ny >= grid[0].length || grid[nx][ny] == '0' || grid[nx][ny] != ch) continue;
            dfs(grid , nx , ny , ch);
        }
    }

    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();
        int m = input.nextInt();
        char[][] grid = new char[n][m];
        for(int i = 0 ; i < n ; i ++){
            String str = input.next();
            char[] temp = str.toCharArray();
            for(int j = 0 ; j < m ; j ++){
                grid[i][j] = temp[j];
            }
        }
        One_ThreeKingdoms threeKingdoms = new One_ThreeKingdoms();
        int ans = threeKingdoms.getForcesNum(grid);
        System.out.println(ans);
    }

}
